package com.usian.controller;

import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.pojo.Job;
import com.usian.pojo.User;
import com.usian.service.DeptService;
import com.usian.service.EmpService;
import com.usian.service.JobService;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private EmpService empService;
    @Autowired
    private JobService jobService;
    @Autowired
    private DeptService deptService;


//     进入列表页

    @RequestMapping("/index")
    public String index(){
        return "index";
    }


//     查询用户权限

    @PostMapping("/userRole")
    @ResponseBody
    public User userRole(HttpServletRequest request){
        //从session获取用户的角色 1超级管理员显示新增部门、职位的权限，其他角色不能新增
        User user = (User) request.getSession().getAttribute("user");
        User user1 =new User();
        user1.setUserName(user.getUserName());
        user1.setRoleId(user.getRoleId());
        return user1;
    }


//     查询列表
    @PostMapping("/findPage")
    @ResponseBody
    public PageResult<Emp> findPage(@RequestBody Map<String,Object> searchMap, int page, int size){
        return  empService.findPage(searchMap,page,size);
    }


//     查询所有岗位


    @PostMapping("/jobList")
    @ResponseBody
    public List<Job> jobList(){
        return jobService.findAllJob();
    }


//      查询所有部门

    @PostMapping("/deptList")
    @ResponseBody
    public List<Dept> deptList(){
        return deptService.findAllDept();
    }


//      根据id查询

    @GetMapping("/findById")
    @ResponseBody
    public Emp findById(Integer id){
        return empService.findById(id);
    }


//     新增和修改

    @PostMapping("/save")
    @ResponseBody
    public int save(@RequestBody Emp emp){
        int result =0;
        if(emp.getId()==null){
            result = empService.save(emp);
        }else{
            result = empService.updateById(emp);
        }
        return result;
    }


//     删除

    @GetMapping("/deleteById")
    @ResponseBody
    public int deleteById(Integer id){
        return empService.deleteById(id);
    }
}
